package receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.sip.SipAudioCall;
import android.telephony.SmsMessage;

import java.util.Objects;

public class SmsBroadcastReceiver extends BroadcastReceiver {


    private OnReceiveSmsListener listener;
    public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

    public SmsBroadcastReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
            Object[] pdus = (Object[]) intent.getExtras().get("pdus");
            for (Object pdu : pdus) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
                String content = smsMessage.getDisplayMessageBody();
                listener.OnReceive(content);
                abortBroadcast();
            }
        }
    }

    public interface OnReceiveSmsListener {
        void OnReceive(String content);
    }
    public void setOnReceiveSmsListener(OnReceiveSmsListener listener) {
        this.listener = listener;
    }

}
